/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.c0910g.Schedule;

import com.myapp.c0910g.Utility.Constant;
import com.myapp.c0910g.entity.ObjectData;
import com.myapp.c0910g.entity.PaymentDetailsFacade;
import com.myapp.c0910g.entity.UserRadioCabFacade;
import it.sauronsoftware.cron4j.Task;
import it.sauronsoftware.cron4j.TaskExecutionContext;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author HaiPham
 */
public class CheckUserPay extends Task {

    PaymentDetailsFacade paymentDetailsFacade = lookupPaymentDetailsFacadeBean();
    UserRadioCabFacade userRadioCabFacade = lookupUserRadioCabFacadeBean();
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");

    @Override
    public void execute(TaskExecutionContext tec) throws RuntimeException {
         try {
            List<ObjectData> list = paymentDetailsFacade.getAllUser_CheckBan_SendEmailByStatus();
            for (int i = 0; i < list.size(); i++) {
                checkStatus(list.get(i));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public boolean updateStatus(Integer userid) {
        int rs1 = paymentDetailsFacade.updateStatusNotPay(userid);
        int rs2 = userRadioCabFacade.updateStatusUser(userid);

        if (rs1 > 0 && rs2 > 0) {
            return true;
        }
        return false;
    }

    private void checkStatus(ObjectData obj) throws Exception {
        if (obj != null) {
            if (obj.getPaymentTypeName().equals(Constant.PAYMENT_TYPE_LISTING)
                    || obj.getPaymentTypeName().equals(Constant.PAYMENT_TYPE_ADVERTISE)
                    || obj.getPaymentTypeName().equals(Constant.PAYMENT_TYPE_DRIVER)) {

                if (obj.getPaymentBy().equals(Constant.PAYMENT_BY_MONTHLY)) {

                    Calendar calCurrent = Calendar.getInstance();//ngay hien tai, h he thong
                    //calCurrent.add(Calendar.MONTH, 1);
                    Calendar calEndDate = Calendar.getInstance();

                    calEndDate.setTime(obj.getPdStartDate());
                    calEndDate.add(Calendar.MONTH, 1);//ngay ket thuc = ngay bat dau + 1 thang

                    String strEndDate = sdf.format(calEndDate.getTime());
                    String strFinishDate = sdf.format(calCurrent.getTime());

                    System.out.println("----Monthly----");
                    if (strEndDate.equals(strFinishDate)) {
                        if (updateStatus(obj.getUrcID())) {
                            System.out.println("Banned nick. Theo Monthly");
                        } else {
                            System.out.println("Banned nick Failed. Theo Monthly");
                        }
                    } else {
                        System.out.println("Don't ban: " + strFinishDate + " not compare " + strEndDate);
                    }

                    calCurrent.add(Calendar.DATE, 3);
                    strFinishDate = sdf.format(calCurrent.getTime());//ngay gui email = ngay hien tai + 3 days

                    if (strEndDate.equals(strFinishDate)) {
                        System.out.println("Send Email. Theo Monthly");
                    } else {
                        System.out.println("Don't Send: " + strFinishDate + " not compare " + strEndDate);
                    }
                } else if (obj.getPaymentBy().equals(Constant.PAYMENT_BY_QUARTERLY)) {

                    Calendar calCurrent = Calendar.getInstance();
                    //calCurrent.add(Calendar.MONTH, 3);
                    Calendar calEndDate = Calendar.getInstance();

                    calEndDate.setTime(obj.getPdStartDate());
                    calEndDate.add(Calendar.MONTH, 3);//ngay ket thuc = ngay bat dau + 3 thang

                    String strEndDate = sdf.format(calEndDate.getTime());
                    String strFinishDate = sdf.format(calCurrent.getTime());

                    System.out.println("----Quarterly----");
                    if (strEndDate.equals(strFinishDate)) {

                        if (updateStatus(obj.getUrcID())) {
                            System.out.println("Banned nick. Theo Quarterly");
                        } else {
                            System.out.println("Banned nick Failed. Theo Quarterly");
                        }
                    } else {
                        System.out.println("Don't ban: " + strFinishDate + " not compare " + strEndDate);
                    }

                    calCurrent.add(Calendar.DATE, 3);
                    strFinishDate = sdf.format(calCurrent.getTime());

                    if (strEndDate.equals(strFinishDate)) {
                        System.out.println("Send Email. Theo Quarterly");
                    } else {
                        System.out.println("Don't Send: " + strFinishDate + " not compare " + strEndDate);
                    }
                } else {
                    System.out.println("Empty");
                }
            }
        }
    }

    @Override
    public boolean canBePaused() {
        return super.canBePaused();
    }

    @Override
    public boolean canBeStopped() {
        return super.canBeStopped();
    }

    @Override
    public boolean supportsCompletenessTracking() {
        return super.supportsCompletenessTracking();
    }

    @Override
    public boolean supportsStatusTracking() {
        return super.supportsStatusTracking();
    }

    private UserRadioCabFacade lookupUserRadioCabFacadeBean() {
        try {
            Context c = new InitialContext();
            return (UserRadioCabFacade) c.lookup("java:global/RadioCabsProject/RadioCabsProject-ejb/UserRadioCabFacade!com.myapp.c0910g.entity.UserRadioCabFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private PaymentDetailsFacade lookupPaymentDetailsFacadeBean() {
        try {
            Context c = new InitialContext();
            return (PaymentDetailsFacade) c.lookup("java:global/RadioCabsProject/RadioCabsProject-ejb/PaymentDetailsFacade!com.myapp.c0910g.entity.PaymentDetailsFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
